package com.ibeetl.admin.core.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.cms.entity.CmsArticle;
import com.ibeetl.admin.core.entity.CoreZan;
import com.ibeetl.cms.service.CmsArticleService;
import com.ibeetl.admin.core.service.CoreZanService;
import com.ibeetl.admin.core.util.ConvertUtil;
import com.ibeetl.admin.core.util.HttpRequestLocal;

import cn.hutool.core.date.DateUtil;

/**
 * 点赞 接口
 */
@Controller
public class CoreZanController{

    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/core/zan";


    @Autowired private CoreZanService zanService;
    @Autowired private CmsArticleService articleService;
    @Autowired private HttpRequestLocal httpRequestLocal;
    
    
    @PostMapping(MODEL + "/zan.json")
    @ResponseBody
    public JsonResult zan(HttpServletRequest request, @RequestParam(value = "aId", required = true) Long aId) {
    	
    	CmsArticle article = this.articleService.queryById(aId);
    	
    	if (article == null) return new JsonResult<>().fail();
    	
    	String type = "zan";
    	Long onehour = 3600*1000L;
    	boolean isValidate = true;
    	
		String ip = httpRequestLocal.getRequestIP();
		
		Long time = new Date().getTime() - onehour;
		
		String strTime = DateUtil.format(new Date(time), "yyyy-MM-dd HH:mm:ss");
		
		Integer validateZan = zanService.validateZan(article.getId(), strTime, ip, type);
		
		
		if (isValidate == false || validateZan == null || validateZan == 0) {
			CoreZan zan = new CoreZan();
			zan.setArticleId(article.getId());
			zan.setIp(ip);
			zan.setPer(1L);//默认是1
			zan.setCreateTime(new Date());
			zan.setType(type);
			this.zanService.upsert(zan);

			Integer count = zanService.getZanByArticleId(article.getId(), type);
			if (count == null) count = 0;
			
			return JsonResult.success(count);
		} else {
			//return new JsonResult().fail("1xiaoshi"); 
			 return JsonResult.failMessage("点赞失败，同一篇文章1小时内不能重复点赞");
		}
		
//		List interActiveList = this.interactiveService.findList("from " + Interactive.class.getName() + " where article.id = ? and attr5 > ? and type = ? and ip = ?", new Object[]{article.getId(), time, type,ip});
//		if (interActiveList.size() > 0) {
//			return this.userService.responseWritePrint(response, "{\"error\":2,\"zanCount\":\"" + article.getZanCount() + "\"}");
//		}
		
    	//coreZanService.save(coreZan);
    }
    

    @PostMapping(MODEL + "/delete.json")
    @Function("core.coreZan.delete")
    @ResponseBody
    public JsonResult delete(String ids) {
        if (ids.endsWith(",")) {
            ids = StringUtils.substringBeforeLast(ids, ",");
        }
        List<Long> idList = ConvertUtil.str2longs(ids);
        zanService.batchDelCoreZan(idList);
        return new JsonResult().success();
    }
    

}
